package com.suez.eimsserver.controller;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.suez.eimsserver.entity.Person;
import com.suez.eimsserver.impl.IPerson;
import com.suez.eimsserver.server.DbUtil;

public class PersonController implements IPerson{

	@Override
	public List<Person> queryAll()  {
		// TODO Auto-generated method stub
		List<Person> list = new ArrayList<Person>();
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null ;
		ResultSet rs = null;
		
		String sql = "select * from person";
		try {
			pst= connection.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				String id = rs.getString(1);
				String name = rs.getString(2);
				SimpleDateFormat   formatter   = new  SimpleDateFormat("yyyy-MM-dd ");
				Date dob;
				dob = (Date) formatter.parse(rs.getString(3));
				
//				java.util.Date date = new java.util.Date(rs.getString(3));
//				Date   dob   =   new Date(date.getTime());
				
				String tel = rs.getString(4);
				Person person = new Person(id, name, dob, tel);
				list.add(person);
			}
			
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freeRS(rs);
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return list;
	}

	@Override
	public int insert(Person p) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		String id = p.getId();
		String name = p.getPname();
		Date dob = p.getDob();
		String tel = p.getTel();
		
		String sql = "insert into person values ('"+id+"','"+name+"','"+dob+"','"+tel+"')";
		System.out.println("sql 语句"+sql);
		int flag=0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return flag;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		
		String sql ="delete person where id = '"+id+"'";
		System.out.println("sql 语句"+sql);
		int flag=0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		return flag;
	}
	public int update(Map<String, Object> mess) {
		// TODO Auto-generated method stub
		Connection connection = DbUtil.getConnection();
		PreparedStatement pst = null;
		String id=(String) mess.get("id");
		
		String sql = getUpdateStr(mess);
		System.out.println("sql 语句"+sql);
		
		int flag = 0;
		try {
			pst = connection.prepareStatement(sql);
			flag = pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DbUtil.freePre(pst);
			DbUtil.freeConn(connection);
		}
		
		
		return flag;
	}
//	@Override
//	public int update(String id, String message) {
//		// TODO Auto-generated method stub
//		Connection connection = DbUtil.getConnection();
//		PreparedStatement pst = null;
//		String sql = getUpdateStr(id, splitMsg(message));
//		
//		int flag = 0;
//		try {
//			pst = connection.prepareStatement(sql);
//			flag = pst.executeUpdate();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			DbUtil.freePre(pst);
//			DbUtil.freeConn(connection);
//		}
//		
//		
//		return flag;
//	}
	
//	public Map<String, String> splitMsg(String message){
//		Map<String, String> msg = new HashMap<String, String>();
//		String arr[] = message.split(",");
//		for(String temp:arr){
//			msg.put(temp.split(":")[0], temp.split(":")[1]);
//		}
//		
//		return msg;
//	}
	
	public String getUpdateStr(Map<String, Object> msg){
		//update TEACHERS set teacher_name='艾玛' where teacher_id ='010';
		String sql="update person set ";
		int flag=0;
		String id = (String)msg.get("id");
		if(msg.get("pname")!=null){
			String pname = (String)msg.get("pname");
			sql = sql + "pname='"+pname+"'";
			flag++;
		}
		if(msg.get("dob")!=null){
			if(flag>0){
				sql = sql +" , ";
			}
			String dob = (String)msg.get("dob");
			sql = sql + "dop=to_date('"+dob+"','YYYY-MM-DD')";
			flag++;
		}
		if(msg.get("tel")!=null){
			if(flag>0){
				sql = sql + " , ";
			}
			String tel = (String)msg.get("tel");
			sql = sql + "tel='"+tel+"'";
		}
		sql = sql + " where id = '"+id+"'";
		return sql;
	}


	
}
